package com.dup.jxlh.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dup.jxlh.admin.entities.model.Contract;
import com.dup.jxlh.admin.entities.model.ContractAttach;
import com.dup.jxlh.admin.entities.vo.ContractVO;
import com.dup.jxlh.admin.mapper.ContractAttachMapper;
import com.dup.jxlh.admin.mapper.ContractMapper;
import com.dup.jxlh.admin.service.AliYunOssService;
import com.dup.jxlh.admin.service.ContractService;
import com.dup.jxlh.admin.service.OrganService;
import com.dup.jxlh.admin.service.OwnValidatorService;
import com.dup.jxlh.api.entities.PaginationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.ArrayList;

@Service("contractServiceImpl")
public class ContractServiceImpl implements ContractService {
    @Resource
    private ContractMapper contractMapper;

    @Resource
    private ContractAttachMapper contractAttachMapper;

    @Autowired
    private OwnValidatorService ownValidatorService;

    @Autowired
    private AliYunOssService aliYunOssService;

    @Autowired
    private OrganService organService;

    @Override
    @Cacheable(value = "contract", key = "#id")
    public Contract getContract(BigInteger id) {
        return contractMapper.selectById(id);
    }

    @Override
    public Page<ContractVO> getContractList(PaginationData paginationData, Contract contract) {
        Page<ContractVO> contractVOPage = new Page<>(paginationData.getCurrent(), paginationData.getSize());
        contractVOPage.setOptimizeCountSql(false);
        QueryWrapper<Contract> queryWrapper = new QueryWrapper<>();

        ownValidatorService.setQueryWrapperByLevel(queryWrapper, paginationData);

        if (!paginationData.getSearch().isEmpty()) {
            queryWrapper.and(i -> i
                    .like("name", paginationData.getSearch())
                    .or()
                    .like("code", paginationData.getSearch()));
        }

        if (contract.getType() != null) {
            queryWrapper.eq("type", contract.getType());
        }

        if (contract.getDepartmentId() != null) {
            queryWrapper.eq("department_id", contract.getDepartmentId());
        }

        if (!StringUtils.isEmpty(contract.getReceivedTime())) {
            queryWrapper.apply("date_format(received_time,'%Y') = {0}", contract.getReceivedTime());
        }

        ArrayList<ContractVO> contractList = contractMapper.getContractList(contractVOPage, queryWrapper);
        contractVOPage.setRecords(contractList);
        return contractVOPage;
    }

    @Override
    public ArrayList<ContractVO> getContractData(PaginationData paginationData, Contract contract) {
        QueryWrapper<Contract> queryWrapper = new QueryWrapper<>();

        ownValidatorService.setQueryWrapperByLevel(queryWrapper, paginationData);

        if (!paginationData.getSearch().isEmpty()) {
            queryWrapper.and(i -> i
                    .like("name", paginationData.getSearch())
                    .or()
                    .like("code", paginationData.getSearch()));
        }

        if (contract.getType() != null) {
            queryWrapper.eq("type", contract.getType());
        }

        if (contract.getDepartmentId() != null) {
            queryWrapper.eq("department_id", contract.getDepartmentId());
        }

        ArrayList<ContractVO> contractList = contractMapper.getContractData(queryWrapper);
        return contractList;
    }

    @Override
    public Boolean addContract(Contract contract) {
        if (contractMapper.insert(contract) == 1) {
            organService.addOrgan(contract.getCompanyId(), contract.getClientCompany());
            organService.addOrgan(contract.getCompanyId(), contract.getConsultCompany());
            return true;
        }
        return false;
    }

    @Override
    @CacheEvict(value = "contract", key = "#contract.id")
    public Boolean editContract(Contract contract) {
        if (contractMapper.updateById(contract) == 1) {
            organService.addOrgan(contract.getCompanyId(), contract.getClientCompany());
            organService.addOrgan(contract.getCompanyId(), contract.getConsultCompany());
            return true;
        }
        return false;
    }

    @Override
    public ContractAttach addContractAttach(ContractAttach contractAttach) {
        if (contractAttachMapper.insert(contractAttach) == 1) {
            return contractAttach;
        }
        return null;
    }

    @Override
    public Boolean delContractAttach(BigInteger id, BigInteger contractId) {
        ContractAttach contractAttach = contractAttachMapper.selectById(id);
        if (!contractId.equals(contractAttach.getContractId())) {
            return false;
        }
        if (contractAttachMapper.deleteById(id) == 1) {
            aliYunOssService.delAliYunOssFile(contractAttach.getUrl());
            return true;
        }
        return false;
    }

}
